/*
 * libwsnpkt.h
 *
 *  Created on: Jul 24, 2010
 *      Author: carlo
 */

#ifndef LIBWSNPKT_H_
#define LIBWSNPKT_H_

#define RET_OK				0
#define RET_ERR_SIZE 		-1
#define RET_ERR_NOTFOUND	-2

#define TERM_BYTE 			0xFF
#define PRE_BYTE			0xFF

#define MAC_SIZE 			8
#define MAX_LEN_S			0x0F
#define MAX_LEN_PKT			127
#define MAX_PREAMB			8

#define EXTR_S_ID(h)		(h & 0x0F)
#define EXTR_S_LEN(h)		((h & 0xF0) >> 4)
#define HEAD_S(id,len)		(id | len << 4)
#define offsetof(st, m) 	((size_t) ( (char *)&((st *)(0))->m - (char *)0 ))
#define alignof(t) 			((sizeof (t) > 1)? offsetof(struct { char c; t x; }, x) : 1)

enum {
	SENS_ID_TEMP,
	SENS_ID_HUM,
	SENS_ID_LIGHT
};

typedef char pkt_size_t;
typedef char pkt_mac_t[MAC_SIZE];
typedef char pkt_seq_t;
typedef char pkt_term_t;

typedef unsigned char sens_data_len_t;
typedef unsigned char sens_id_t;
typedef char sens_data_t[MAX_LEN_S];

typedef unsigned char pkt_preamb_t;

typedef unsigned char sens_head_t;

typedef struct {
	sens_head_t header;
	sens_data_t data;
} sens_field_t;

typedef struct {
	pkt_size_t size;
	pkt_mac_t mac;
	pkt_seq_t seq;
} pkt_head_t;


extern pkt_size_t pkt_init(unsigned char *pkt_buf, pkt_size_t dim, pkt_size_t pr_dim);
extern pkt_size_t pkt_set_sensor(unsigned char *pkt_buf, pkt_size_t dim, sens_id_t id, char *data, sens_data_len_t n_bytes);
extern pkt_size_t pkt_get_sensor(unsigned char *pkt_buf, sens_id_t id, char *data, sens_data_len_t n_bytes);
extern pkt_size_t pkt_set_mac(unsigned char *pkt_buf, pkt_mac_t mac, pkt_size_t len);
extern pkt_size_t pkt_get_mac(unsigned char *pkt_buf, pkt_mac_t mac, pkt_size_t len);
extern pkt_size_t pkt_set_seq(unsigned char *pkt_buf, pkt_seq_t seq);
extern pkt_seq_t  pkt_get_seq(unsigned char *pkt_buf);
extern pkt_size_t  pkt_get_size(unsigned char *pkt_buf);
extern char pkt_get_sensors_list(unsigned char *pkt_buf, unsigned char *ext_buf, pkt_size_t dim);

#endif /* LIBWSNPKT_H_ */
